;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.SapStock", [])
        .controller("SapStockCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                curRow: 0
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.detailPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.dataList = [];
            $scope.detailDataList = [];
            $scope.materialInfo = {
                materialNo: "",
                locationCode: ""
            };
            $scope.areaCode = "";
            $scope.warehouseCode = "";
            $scope.status = "";
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/sapInventory/exportAll")
            };
            $scope.tableConfig = {
                fixedWidth: 150,
                scrollWidth: 1300,
                tableCol: [
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 150,
                        isShow: true,
                        isFixed: true,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "工厂",
                        dataField: "plantCode",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "库存地点",
                        dataField: "location",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "非限制库存",
                        dataField: "stockQty",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "同步时间",
                        dataField: "syncTimeStr",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                ]
            };


            function initData() {
                $scope.detailDataList = [];
                $scope.materialInfo = {
                    materialNo: "",
                    locationCode: ""
                };
            }

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            var btnPermissionCode = {
                depotStockExport: window.ActionCode.depotStockExport
            };

            $scope.btnPermission = {
                depotStockExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/sapInventory/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.pageInfo.list, function (item) {
                        item.isChecked = false;
                        item.statusShow = $filter('storageType')(item.status);
                    });
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach(resp.value.pageInfo.list, function (item) { 
                        item.syncTimeStr = $filter("YbDate")(item.syncTime, df);
                    });

                    $scope.pageInfo.totalRows = resp.value.pageInfo.total;
                    $scope.pageInfo.totalPages = resp.value.pageInfo.pages;
                    $scope.dataList = resp.value.pageInfo.list;
                });
            }
            //同步SAP接口
            $scope.syncSapInventory = function (toolbar) { 
                var service = "device";
                var reqUrl = "/sapSync/syncInventory";
                var param = {}; 
                ybHttp.Post(service, reqUrl, {}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    getDataList(1)
                });
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };


            function intentionDetail(item) {
                var param = {
                    materialsNo: item.materialNo
                };
                ybWidget.IntentionDetail(param);
            }

            function deliveryDetail(item) {
                var param = {
                    materialsNo: item.materialNo,
                    warehouseCode: item.warehouseCode
                };
                ybWidget.DeliveryDetail(param);
            }



            $scope.searchDetail = function () {
                viewDetail($scope.detailPageInfo.curPage);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                localStorage.setItem($scope.storageKey, JSON.stringify($scope.condition));
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            (function () {
                $scope.storageKey = $state.current.name;
                UserContextService.LocalCache($scope.storageKey, $scope.condition);

                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);